﻿// Copyright © 2011 Paul Vanukoff (pvanukoff@gmail.com)

using System;

namespace Bravura.Native.PluginBase.Waveforms
{
	/// <summary>
	/// Triangle waveform with variable width.
	/// The width allows transitioning from a sawtooth (0.0 or 1.0) to a triangle (0.5).
	/// </summary>
	public class TriangleWaveform : Waveform
	{
		private double _width = 0.50;
		private double _upSlope = 0.0;
		private double _dnSlope = 0.0;

		public TriangleWaveform()
		{
			SetWidth(0.50);
		}

		public void SetWidth(double width)
		{
			_width = width;
			_upSlope = 2.0 / width;
			_dnSlope = -2.0 / (1.0 - width);
		}

		#region Waveform Implementation

		public override double GetValue(double pos)
		{
			return (pos < _width) ? (-1.0 + _upSlope * pos) : (1.0 + _dnSlope * (pos - _width));
		}

		#endregion
	}
}
